package com.efast.dmom.mgmt.controller.les.pda;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.bean.les.GoodsReceiptBean;
import com.efast.dmom.mgmt.entity.les.TwDocAsnDetails;
import com.efast.dmom.mgmt.entity.les.TwDocAsnHeader;
import com.efast.dmom.mgmt.service.les.TwDocAsnDetailsService;
import com.efast.dmom.mgmt.service.les.TwDocAsnHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;

import java.util.List;

/**
 * @author chlp
 */
@Controller
@RequestMapping(value = "/pdaTwDocAsnDetails")
@Api(description = "Pda->收货->明细收货")
public class PdaTwDocAsnDetailsController {

	private final TwDocAsnHeaderService twDocAsnHeaderService;
	private final TwDocAsnDetailsService twDocAsnDetailsService;

	@Autowired
	public PdaTwDocAsnDetailsController(TwDocAsnHeaderService twDocAsnHeaderService,
			TwDocAsnDetailsService twDocAsnDetailsService) {
		this.twDocAsnHeaderService = twDocAsnHeaderService;
		this.twDocAsnDetailsService = twDocAsnDetailsService;
	}

	/**
	 * 【确认收货】 HEADER的接收:ASN HEADER的接收[ASN状态改为2(即收货完成),收货完成后在TW_INBOUND_ALLOC_DETAIL创建数据]
	 *
	 * @param goodsReceiptBean
	 *            {"asnSheetNo":"xxx"}
	 */
	@RequestMapping(value = "/headerReceive", method = RequestMethod.POST)
	@ApiOperation(value = "【确认收货】", notes = "请求参数：")
	public @ResponseBody ResultBean headerReceive(@RequestBody GoodsReceiptBean goodsReceiptBean) {
		ResultBean result = new ResultBean(true, "'");
		List<TwDocAsnDetails> listTwDocAsnDetails = twDocAsnDetailsService.selectTwDocAsnDetails(new Assist(
				Assist.andEq("asn_sheet_no", goodsReceiptBean.getListTwDocAsnDetails().get(0).getAsnSheetNo())));
		goodsReceiptBean.setListTwDocAsnDetails(goodsReceiptBean.getListTwDocAsnDetails());
		List<TwDocAsnHeader> twDocAsnHeaderList = twDocAsnHeaderService.selectTwDocAsnHeader(new Assist(
				Assist.andEq("asn_sheet_no", goodsReceiptBean.getListTwDocAsnDetails().get(0).getAsnSheetNo())));
		if (twDocAsnHeaderList.size() == 0) {
			throw new ServiceException("根据asn号无法查询到记录");
		}
		goodsReceiptBean.setTwDocAsnHeader(twDocAsnHeaderList.get(0));
		twDocAsnHeaderService.headerReceive(goodsReceiptBean);
		return result;
	}

}
